import config from "./config";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchList: [], // 查询条件
    rightInfos: [], // 右侧展示的数据
    leftKey: '' // 左侧选中的项的key值
  },
  selectLeft(e) {
    const key = e.currentTarget.dataset.key
    const rightInfos = this.data.searchList.find(val => val.key === key)
    this.setData({
      rightInfos: rightInfos.children,
      leftKey: key
    })
  },
  selectOne(e) {
    // console.log('e', e)
    const rightOne = this.data.rightInfos.find(val => val.key === e.currentTarget.dataset.parentkey)
    rightOne.showKey = e.currentTarget.dataset.key
    rightOne.showValue = e.currentTarget.dataset.value
    rightOne.showLabel = e.currentTarget.dataset.label
    const leftIndex = this.data.searchList.findIndex(val => val.key === this.data.leftKey)
    this.data.searchList[leftIndex].children = this.data.rightInfos
    // console.log(this.data.leftKey, 'leftKey')
    this.setData({
      rightInfos: this.data.rightInfos,
      searchList: this.data.searchList
    })
    // console.log(this.data.rightInfos, 'rightInfos')
    // console.log(this.data.searchList, 'searchList')
  },

  selectOneMul(e) {
    const rightOne = this.data.rightInfos.find(val => val.key === e.currentTarget.dataset.parentkey)
    rightOne.showKey = e.currentTarget.dataset.key
    // 可取消
    const valueOne = rightOne.showValue.findIndex(pre => pre == e.currentTarget.dataset.value)
    if (valueOne > -1) {
      rightOne.showValue.splice(valueOne, 1)
    } else {
      rightOne.showValue.push(e.currentTarget.dataset.value)
    }
    const labelOne = rightOne.showLabel.findIndex(pre => pre == e.currentTarget.dataset.label)
    if (labelOne > -1) {
      rightOne.showLabel.splice(labelOne, 1)
    } else {
      rightOne.showLabel.push(e.currentTarget.dataset.label)
    }
    console.log(rightOne, 'rightOnerightOnerightOne');
    const leftIndex = this.data.searchList.findIndex(val => val.key === this.data.leftKey)
    this.data.searchList[leftIndex].children = this.data.rightInfos
    this.setData({
      rightInfos: this.data.rightInfos,
      searchList: this.data.searchList
    })
    // console.log(this.data.rightInfos, 'rightInfos')
    // console.log(this.data.searchList, 'searchList')
  },
  // 数据重置
  resetData() {
    let list = []
    if (this.data.type == 1) {
      list = config.getSearchAllList()
    } else {
      list = config.getSearchList()
    }
    const searchList = list
    const rightInfos = searchList.find(val => val.key === this.data.leftKey)
    this.setData({
      searchList: list,
      rightInfos: rightInfos.children,
    })
  },
  // 确定筛选
  async confirm() {
    const vipCode = await getApp().globalData.checkVipStatus()
    // if (vipCode === 'ordinary' || vipCode === 'notVipBindFI' || vipCode === 'visitor') {
    //   wx.showModal({
    //     title: '提示',
    //     content: '该功能为VIP会员功能，请联系客服了解详情。',
    //     showCancel: false,
    //     confirmText: '确定',
    //     success: function () {}
    //   })
    //   return
    // }
    // 获取所有的筛选条件
    let selectedObj = {}
    let labelList = []
    // console.log(this.data.searchList, 'this.data.searchList')
    this.data.searchList.forEach(val => {
      val.children.forEach(pre => {
        // 获取有值的项
        if (pre.showKey) {
          const labelone = Array.isArray(pre.showLabel) ? pre.showLabel.length > 0 ? pre.showLabel.join('、'): '' : pre.showLabel
          if(labelone) {
            labelList.push({
              name: pre.label,
              value: labelone
            })
          }
          // 特殊字段的处理
          switch (pre.showKey) {
            case 'yearsOfEstablishUpperLimit':
              selectedObj['yearsOfEstablishLowerLimit'] = pre.showValue.split('_')[0]
              selectedObj['yearsOfEstablishUpperLimit'] = pre.showValue.split('_')[1]
              break;
            case 'registeredCapitalUpperLimit':
              selectedObj['registeredCapitalLowerLimit'] = pre.showValue.split('_')[0]
              selectedObj['registeredCapitalUpperLimit'] = pre.showValue.split('_')[1]
              break;
            case 'debtUpperLimit':
              selectedObj['debtLowerLimit'] = pre.showValue.split('_')[0]
              selectedObj['debtUpperLimit'] = pre.showValue.split('_')[1]
              break;
            case 'numberOfCompanyUpperLimit':
              selectedObj['numberOfCompanyLowerLimit'] = pre.showValue.split('_')[0]
              selectedObj['numberOfCompanyUpperLimit'] = pre.showValue.split('_')[1]
              break;
            case 'loanBankNumUpperLimit':
              selectedObj['loanBankNumLowerLimit'] = pre.showValue.split('_')[0]
              selectedObj['loanBankNumUpperLimit'] = pre.showValue.split('_')[1]
              break;
            case 'scoreUpperLimit':
              selectedObj['scoreLowerLimit'] = pre.showValue.split('_')[0]
              selectedObj['scoreUpperLimit'] = pre.showValue.split('_')[1]
              break;
            default:
              selectedObj[pre.showKey] = pre.showValue
              break;
          }
        }
      })
    })
    console.log(labelList, 'labelListlabelListlabelListlabelList');
    wx.setStorageSync('labelList', labelList)
    // 跳转到列表页面
    console.log(selectedObj, 'selectedObj')
    const saveInfos = JSON.stringify(selectedObj)
    wx.navigateTo({
      url: `/pages/companyList/companyList?selectedObj=${saveInfos}&type=${this.data.type}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const type = options.type
    this.setData({
      type: type
    })
    if (this.data.type == 1) {
      // 全量搜索
      wx.setNavigationBarTitle({
        title: '全量搜索',
      });
      this.setData({
        searchList: config.getSearchAllList()
      })
    } else {
      wx.setNavigationBarTitle({
        title: '高级筛选',
      });
      this.setData({
        searchList: config.getSearchList()
      })
    }
    // 默认展示第一个
    const rightInfos = this.data.searchList[0]
    this.setData({
      rightInfos: rightInfos.children,
      leftKey: this.data.searchList[0].key
    })
    console.log(this.data.searchList, 'searchList')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});